package com.study_platform.controller;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.study_platform.entity.Friend;
import com.study_platform.service.FriendService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/study_platform/friend")
public class FriendController {
    @Resource
    private FriendService friendService;

    @PostMapping("/add/")
    public JSONObject add(@RequestBody Friend friend){
        JSONObject jsonObject = new JSONObject();
        if (friendService.save(friend)) {
            jsonObject.put("error_message", "success");
        }else{
            jsonObject.put("error_message", "error");
        }
        return jsonObject;
    }

    @DeleteMapping("/delete/")
    public JSONObject delete(@RequestBody Friend friend){
        JSONObject jsonObject = new JSONObject();
        LambdaQueryWrapper<Friend> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Friend::getUserId1, friend.getUserId1());
        queryWrapper.eq(Friend::getUserId2, friend.getUserId2());
        if (friendService.remove(queryWrapper)){
            jsonObject.put("error_message", "success");
        }else{
            jsonObject.put("error_message", "error");
        }
        return jsonObject;
    }
}
